package com.nbicc.controller;

import com.nbicc.pojo.User;
import com.nbicc.service.UserService;
import com.nbicc.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/5/10 22:02
 * @Version 1.0 (版本)
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public JsonData login(User user,HttpSession session){
        System.out.println("---login---");
        String uname=user.getUname();
        String upwd=user.getUpwd();
        System.out.println(user.getUname()+" "+user.getUpwd());
        JsonData jsonData = new JsonData();
        User u = userService.login(user);
        session.setAttribute("u",u.getUname());
        if (!u.getUpwd().equals(user.getUpwd())) {
            System.out.println("失败");
            return jsonData.buildFailure();
        }else {
            System.out.println("成功");
            return jsonData.buildSuccess(u.getUname());
        }
    }
    /**
     * 注销
     */
    @GetMapping("/out")
    public JsonData out(HttpSession session){
        System.out.println("--out--");
        JsonData jsonData = new JsonData();
        session.getAttribute("u");
        session.invalidate();
        return jsonData.buildSuccess();
    }
}
